var selectedItems = fl.getDocumentDOM().library.getSelectedItems();
var allItems = fl.getDocumentDOM().library.items;

var k = 0;
var n = selectedItems.length;
for(; k < n; k++) {
	alert(selectedItems[k].name);
}

function swapItemNames() {
	if(selectedItems.length > 2 && !isFolderIn(selectedItems)) {
		alert("You have selected too many items");
		return;
	}
	
	// happens when you select 2 folders or 1 folder and a symbol or more than 2 items
	if(selectedItems.length > 2 && isFolderIn(selectedItems)) {
		// select the first item to check if it's a folder and how many items it contains
		fl.getDocumentDOM().library.selectItem(selectedItems[0].name);
		var nbItems0 = fl.getDocumentDOM().library.getSelectedItems().length;
		
		// select the second item
		fl.getDocumentDOM().library.selectItem(selectedItems[1].name);
		var nbItems1 = fl.getDocumentDOM().library.getSelectedItems().length;
		
		// if more than 2 items are selected
		if(nbItems0 + nbItems1 < selectedItems.length) {
			alert("You have selected too many items");
			return;
		} else {
			selectedItems = [selectedItems[0], selectedItems[1]];
		}
	}
	if(selectedItems.length == 2) {
		var item0 = selectedItems[0];
		var item1 = selectedItems[1];
		
		var name0 = getItemName(item0);
		var name1 = getItemName(item1);
		
		var i = 0;
		
		do {
			nameTmp = name0 + i;
			i++;
		} while(nameExists(nameTmp))
		
		item0.name = nameTmp;
		item1.name = name0;
		item0.name = name1;
	}
}

function getItemName(item) {
	var fullName = item.name;
	return fullName.substring(fullName.lastIndexOf("/") + 1, fullName.length);
}

function nameExists(nameToSearch) {
	var i = 0;
	var nbItems = allItems.length;
	for(; i < nbItems; i++) {
		if(getItemName(allItems[i]).toLowerCase() == nameToSearch.toLowerCase()) {
			return true;
		}
	}
	return false;
}

function isFolderIn(itemsList) {
	var i = 0;
	var nbItems = itemsList.length;
	for(; i < nbItems; i++) {
		if(itemsList[i].itemType == "folder") {
			return true;
		}
	}
	return false;
}

swapItemNames();